<template>
  <div class="full-screen-container">
    <div class="login-container">
      <el-form :model="loginForm" @submit.prevent="submitForm">
        <el-form-item label="用户名">
          <el-input v-model="loginForm.userName"></el-input>
        </el-form-item>
        <el-form-item label="密码">
          <el-input type="password" v-model="loginForm.password"></el-input>
        </el-form-item>
        <el-form-item>
          <div class="form-actions">
            <el-button type="primary" @click="submitForm">登录</el-button>
            <el-button @click="navigateToRegister">注册</el-button>
          </div>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>
import { ref } from 'vue';
import { useRouter } from 'vue-router';
import { useStore } from 'vuex';
import axios from 'axios';
import { ElMessage } from 'element-plus';

export default {
  name: 'UserLogin',
  setup() {
    const loginForm = ref({
      userName: '',
      password: '',
    });

    const router = useRouter();
    const store = useStore();

    const submitForm = () => {
      axios.post('/api/user/login', loginForm.value)
        .then(response => {
          const result = response.data;
          if (result.code === 1) {
            localStorage.setItem('userToken', result.data.token); 
            ElMessage.success('登录成功');
            store.commit('setUser', 
            { id: result.data.userId, 
              token: result.data.token });
            router.push('/location');
          } else {
            ElMessage.error(result.msg || '登录失败');
          }
        })
        .catch(error => {
          ElMessage.error('登录请求异常: ' + error.message);
        });
    };

    const navigateToRegister = () => {
      router.push('/register'); 
    };

    return {
      loginForm,
      submitForm,
      navigateToRegister
    };
  }
};
</script>

<style scoped>
.full-screen-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background: url('../assets/背景2.jpg') no-repeat center center fixed;
  background-size: cover;
}

.login-container {
  width: 360px;
  padding: 40px;
  box-shadow: 0 8px 30px rgba(0,0,0,0.5);
  border-radius: 12px;
  background: rgba(255, 255, 255, 0.9);
  backdrop-filter: blur(12px);
  color: #333;
  transition: all 0.3s ease-in-out;
}

.login-container:hover {
  box-shadow: 0 10px 40px rgba(0,0,0,0.6);
}

.el-input,
.el-button {
  margin-bottom: 20px;
}

.el-input {
  border: 2px solid #ccc;
  border-radius: 8px;
  background-color: #fff;
  padding: 10px 15px;
  color: #333;
  transition: border-color 0.3s;
}

.el-input:focus {
  border-color: #6454f0;
}

.el-button {
  border: 20px;
  border-radius: 10px;
  width: 50px;
  background-color: #9c94e2;
  transition: background-color 0.3s, transform 0.2s;
  border-color: rgb(219, 140, 140);
  font-size: 15px;
}

.el-button:hover {
  background-color: #a0d2da;
  transform: translateY(-2px);
}

.form-actions {
  display: flex;
  justify-content: space-between;
}
</style>




